package com.henu.engineer.controller.general

import com.alibaba.fastjson.JSONObject
import com.henu.engineer.StringUtils
import com.henu.engineer.service.LoginService
import com.henu.engineer.service.UserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import javax.servlet.http.HttpSession

/**
 * 对于所有用户的个人信息修改
 * @author noexcept
 * @since 2021/8/11 13:51
 */
@RestController
@RequestMapping("/ga")
class UserController {

    @Autowired
    lateinit var userService: UserService

    @Autowired
    lateinit var loginService: LoginService

    /**
     * 查询学生信息
     */
    @GetMapping("/student")
    fun getStudent(studentId: String?) {


        loginService.currentStudent
    }

    /**
     * 查询教师信息
     */
    @GetMapping("/teacher")
    fun getTeacher() = loginService.currentTeacher

    @PostMapping("/updatePwd")
    fun updatePwd(@RequestBody jsonObject: JSONObject?): String {
        val res = JSONObject()
        if (jsonObject == null) {
            res["code"] = 0
            res["msg"] = "json parameter parse error"
            return res.toJSONString()
        }
        val oldPwd = jsonObject["oldPwd"] as String?
        val newPwd = jsonObject["newPwd"] as String?
        if (StringUtils.isEmpty(oldPwd, newPwd)) {
            res["code"] = 0
            res["msg"] = "parameters are incomplete"
            return res.toJSONString()
        }
        val s = userService.updatePwd(loginService.currentUserId!!, oldPwd!!, newPwd!!)
        res["msg"] = s
        if (s != "SUCCESS")
            res["code"] = 0
        else res["code"] = 1
        return res.toJSONString()
    }
}